package com.tydh.webservice.controller;


import com.alibaba.csb.sdk.HttpCaller;
import com.google.gson.Gson;
import com.tydh.webservice.bean.ParamSettingBean;
import com.tydh.webservice.bean.ScanPackByCsbBean;
import com.tydh.webservice.common.Assert;
import com.tydh.webservice.common.utils.AllSetParam;
import com.tydh.webservice.common.utils.Interfacetype;
import com.tydh.webservice.result.R;
import com.tydh.webservice.result.ResultEnum;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

/**
 * 此接口主要用于计算揽投配发扫描信息。
 */
@SuppressWarnings("all")
@CrossOrigin
@Slf4j
@RestController
@RequestMapping("/all/scan")
public class ScanPackByCsb {

    @Autowired
    private AllSetParam allSetParam;

    @Autowired
    private ParamSettingBean paramSettingBean;

    @RequestMapping("/scanPackByCsb")
    public R scanPackByCsb(@RequestBody ScanPackByCsbBean scanPackByCsbBean)
    {
        Gson gson = new Gson();
        Map<String,String> params = new HashMap<String,String>();

        Map messageHeader = allSetParam.setMessageHeader(gson.toJson(scanPackByCsbBean));

        //messageHeader转为json
        String jsonMessageHeader = gson.toJson(messageHeader);
        params.put("messageHeader", jsonMessageHeader);

        String pcsDespatchQuery = gson.toJson(scanPackByCsbBean);
        params.put("pcsDespatchQuery", pcsDespatchQuery);

        String result;
        try {
            result = HttpCaller.doPost(Interfacetype.URL, Interfacetype.LANSHOUPEIFA_NAME, paramSettingBean.getVersion(), params, paramSettingBean.getAk(), paramSettingBean.getSk());
            if (result != null) {
                result = result.replaceAll("\\\\","");
            }
            log.info("新一代的数据{}", result);
            Assert.notNull(result, ResultEnum.ALIYUN_RESPONSE_FAIL);
            return  R.ok().data("data", result);
        } catch (Exception ie) {
            log.info(ie.getMessage());
            return R.error().data("error", ie.getMessage());
        }
    }
}
